﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace DYWebApplication
{
    /// <summary>
    /// BMWebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class BMWebService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public DataSetDB.USERSDataTable GetUserLogin(string username, string userPwd)
        {
            DataSetDBTableAdapters.USERSTableAdapter ad = new DataSetDBTableAdapters.USERSTableAdapter();
            var data = ad.GetDataByUserNamePwd(username, userPwd);
            return data;
        }


        [WebMethod]
        public DataSetDB.AREASDataTable AddAreaName(string username, string home)
        {
            DataSetDBTableAdapters.AREASTableAdapter ad = new DataSetDBTableAdapters.AREASTableAdapter();
            var data = ad.GetData();
            var count = data.Count(wh => wh.AREANAME == username && wh.HOMEAREA == home);
            if (count < 1)
            {
                data.AddAREASRow(username, home);
                ad.Update(data);
            }

            return data;
        }

        [WebMethod]
        public DataSetDB.AREASDataTable DelAddAreaName(string username)
        {
            DataSetDBTableAdapters.AREASTableAdapter ad = new DataSetDBTableAdapters.AREASTableAdapter();
            var data = ad.GetData();
            var count = data.Where(wh => wh.AREANAME == username);
            if (count.Count() > 0)
            {
                count.First().Delete();
                ad.Update(data);
            }
            data = ad.GetData();
            return data;
        }


        [WebMethod]
        public DataSetDB.PERSIONSDataTable GetBaoMingData(string areaName)
        {
            DataSetDBTableAdapters.PERSIONSTableAdapter ad = new DataSetDBTableAdapters.PERSIONSTableAdapter();
            if (areaName.Trim() == "")
            {
                return ad.GetData();
            }
            else
            {
                return ad.GetDataByAREA(areaName);
            }

        }

        [WebMethod]
        public DataSetDB.PERSIONSDataTable UpdateStatus(int id)
        {
            DataSetDBTableAdapters.PERSIONSTableAdapter ad = new DataSetDBTableAdapters.PERSIONSTableAdapter();
            var data = ad.GetData();
            foreach (DataSetDB.PERSIONSRow item in data.Rows)
            {
                if (item.ID == id)
                {
                    if (item.STATUS == "已审核")
                    {
                        item.STATUS = "已付款";
                    }
                    else if (item.STATUS == "未审核")
                    {
                        item.STATUS = "已审核";
                    }
                    else if (item.STATUS == "已付款")
                    {
                        item.STATUS = "未审核";
                    }

                    ad.Update(data);
                    return ad.GetData();
                }
            }
            return ad.GetData();
        }
        [WebMethod]
        public DataSetDB.PERSIONSDataTable DeleteRow(int id)
        {
            DataSetDBTableAdapters.PERSIONSTableAdapter ad = new DataSetDBTableAdapters.PERSIONSTableAdapter();

            try
            {
                var data = ad.GetData();
                foreach (DataSetDB.PERSIONSRow item in data.Rows)
                {
                    if (item.ID == id)
                    {
                        item.Delete();
                        ad.Update(data);
                        return ad.GetData();
                    }
                }
            }
            catch (Exception ex)
            {


            }
            return ad.GetData();

        }
        [WebMethod]
        public DataSetDB.USERSDataTable deleteUser(string areaName)
        {
            DataSetDBTableAdapters.USERSTableAdapter ad = new DataSetDBTableAdapters.USERSTableAdapter();
            DataSetDB.USERSDataTable adGetData = ad.GetData(); ;
            foreach (DataSetDB.USERSRow item in adGetData)
            {
                if (item.LOGINNAME == areaName)
                {
                    item.Delete();
                }

            }
            ad.Update(adGetData);
            return ad.GetData();
        }


        [WebMethod]
        public DataSetDB.USERSDataTable AddUser(DataSetDB.USERSDataTable dataNew)
        {
            DataSetDBTableAdapters.USERSTableAdapter ad = new DataSetDBTableAdapters.USERSTableAdapter();
            DataSetDB.USERSDataTable adGetData = ad.GetData(); ;
            foreach (DataSetDB.USERSRow item in dataNew)
            {
                var cout = adGetData.Count(wh => wh.LOGINNAME == item.LOGINNAME);
                if (cout < 1)
                {
                    adGetData.AddUSERSRow(item.LOGINNAME, item.LOGINPWD, item.USERNAME, item.AREA);
                }

            }
            ad.Update(adGetData);
            return ad.GetData();
        }


    }
}
